#第三题 自动取款机

#初始化变量,存放银行账户信息
accountList=[
    {"account":"BOC0001","password":"930108","fund":10000,"bank":1,"name":"王欢","sex":"先生"},
    {"account":"ABC0001","password":"921216","fund":900,"bank":2,"name":"王欢","sex":"先生"},
    {"account":"ICBC0001", "password": "921216", "fund": 50000, "bank": 3,"name":"王欢","sex":"先生"},
    {"account":"CCB0001","password":"921216","fund":5000,"bank":4,"name":"王欢","sex":"先生"}
]
bankList=[
    {"bankId":1,"bankName":"中国银行","identify":"BOC"},
    {"bankId":2,"bankName":"农业银行","identify":"ABC"},
    {"bankId":3,"bankName":"工商银行","identify":"ICBC"},
    {"bankId":4,"bankName":"建设银行","identify":"CCB"}
]

errCode={"1":"登录成功","0":"账号不存在","-1":"请插入正确的银行卡","-2":"密码错误"}

#需要用到的函数

#获取对象
def FindDict(key, val, list):
    info = ''
    for item in list:
        if (item[key] == val):
            info = item
            break
    return info;

#存取款操作
def Deposit(account,money):
    accountinfo = FindDict("account",account,accountList)
    fund = float(accountinfo["fund"])
    fund+= float(money)
    if fund>0:
        accountinfo["fund"]=fund
        return accountinfo["fund"]

#获取账户信息
def Login(account,password,bankId):
    accountinfo= FindDict('account',account,accountList)
    if accountinfo=='':
        return '0' #账号不存在
    if not accountinfo["bank"]==int(bankId):
        return '-1' #请插入正确的银行卡
    if (accountinfo["password"] == password):
        return '1'
    else:
        return '-2' #密码错误

#操作完一个动作后，是否继续
def Continue():
    print("1：继续操作    2：退出")
    action = input()
    if action=="1":
        print("请选择您要进行的操作：")
        AccountHandle()
    else:
        print("请拔出您的银行卡")

#整个操作的函数,为循环操作
def AccountHandle():
    print('1：查询余额        2：取款')
    print('3：存款            4：退出')
    action = input()
    if action == '1':
        print('您的账户余额为%f' % float(accountinfo["fund"]))
        Continue()
    elif action == '2':
        money = input('请输入取款金额:')
        while float(money) > float(accountinfo["fund"]):
            money = input("银行卡余额不足,请重新输入：")
        else:
            tempFund = Deposit(accountinfo["account"], '-%s' % money)
            print("取款成功,银行卡余额为%f" % float(tempFund))
            Continue()
    elif action == '3':
        money = input('请插入纸币')
        while not money.isnumeric():
            money = input('请输入正确的金额')
        print('1.继续    2.取消')
        action = input()
        if action == "1":
            tempFund = Deposit(accountinfo["account"], money)
            print('存款成功,账户余额为：%f' % tempFund)
            Continue()
        elif action=="2":
            Continue()
    elif action == '4':
        print('请拔出您的银行卡')



print("请插入您的银行卡进行操作：")
print("1:插卡  |  2：离开")
action = input()
if(action=="2"):
    print("欢迎下次光临")
else:
    print("请选择银行:")
    for item in bankList:
        print("%s:%s"%(item["bankId"],item["bankName"]))
    bankId=input()
    account=input("请输入您的账号:")
    password=input("请输入您的密码:")

    #判断账户信息是否正确
    loginStatus = Login(account,password,bankId)
    if not loginStatus=='1':
        print(errCode[loginStatus])
    else:
        accountinfo = FindDict('account',account,accountList)
        print('尊敬的%s%s,请选择您要进行的操作：'%(accountinfo["name"],accountinfo["sex"]))
        AccountHandle()